﻿using UnityEngine;
using System.Collections.Generic;

namespace UI
{
    public class UILuckPopupBox : MonoBehaviour
    {

        public GameObject template;
        //四个位置
        public Transform trans0, trans1, trans2, trans3;

        //激活
        public GameObject inActived1, inActived2, inActived3;
        public GameObject actived0,actived1, actived2, actived3;
        /// <summary>
        /// Mask
        /// </summary>
        public UITexture mask1, mask2, mask3;

        public UILabel name0, name1, name2, name3;
        
        public UILabel titleName;
        public UILabel luckDes;

        List<GameObject> cached = new List<GameObject>();

        #region 数据
        string luckId;
        bool actived;
        string heroId;
        List<string> heroIds;
        Dict.DataModel.TableYuan luck;
        #endregion

        #region 字典字体
        //string dictStr, attStr, defStr, lifeStr;
        #endregion

        void Awake()
        {
            titleName.text = TableTextBlo.GetText("luck_hero");
            //dictStr = TableTextBlo.GetText("luck_text_template");

            //attStr = TableTextBlo.GetText("word_att");
            //defStr = TableTextBlo.GetText("word_def");
            //lifeStr = TableTextBlo.GetText("word_hp");
        }

        public void OnSubmitClick()
        {
            Close();
        }

        public void Init(string luckId,bool actived)
        {
            this.luckId = luckId;
            this.actived = actived;

            luck = Dict.DictDataManager.Instance.tableYuanDao.GetById(luckId);
            string heroId = luck.heroId;
            List<string> heroIds = new List<string>();
            if (!string.IsNullOrEmpty(luck.hero0)) heroIds.Add(luck.hero0);
            if (!string.IsNullOrEmpty(luck.hero1)) heroIds.Add(luck.hero1);
            if (!string.IsNullOrEmpty(luck.hero2)) heroIds.Add(luck.hero2);
            Init(heroId, heroIds);
        }

        private void Init(string heroId,List<string> heroIds)
        {
            this.heroId = heroId;
            this.heroIds = heroIds;

            SetHeroIcons();
            SetHeroNames();
            SetLuckDes();
            SetActived();
            SetMasked();
        }

        private void SetHeroIcons()
        {
            SetHeroIcon(heroId, trans0);

            if(heroIds.Count==1)
            {
                SetHeroIcon(heroIds[0], trans1);
            }
            else if (heroIds.Count == 2)
            {
                SetHeroIcon(heroIds[0], trans1);
                SetHeroIcon(heroIds[1], trans2);
            }
            else if (heroIds.Count == 3)
            {
                SetHeroIcon(heroIds[0], trans1);
                SetHeroIcon(heroIds[1], trans2);
                SetHeroIcon(heroIds[2], trans3);
            }
        }

        public void SetHeroNames()
        {
            name0.text = "";
            name1.text = "";
            name2.text = "";
            name3.text = "";
            

            name0.text = CommonMethod.GetHeroName(heroId);
            if (heroIds.Count >= 1) name1.text = CommonMethod.GetHeroName(heroIds[0]);
            if (heroIds.Count >= 2) name2.text = CommonMethod.GetHeroName(heroIds[1]);
            if (heroIds.Count >= 3) name3.text = CommonMethod.GetHeroName(heroIds[2]);
        }
        
        protected void SetLuckDes()
        {
            luckDes.text = CommonMethod.GetLuckDesTex(luckId, actived);
        }

        protected void SetActived()
        {
            inActived3.SetActive(false);
            inActived2.SetActive(false);
            inActived1.SetActive(false);

            actived0.SetActive(false);
            actived1.SetActive(false);
            actived2.SetActive(false);
            actived3.SetActive(false);

            if (heroIds.Count >= 3)
            {
                inActived3.SetActive(true);
                inActived2.SetActive(true);
                inActived1.SetActive(true);
                if (actived)
                {
                    actived3.SetActive(true);
                    actived2.SetActive(true);
                    actived1.SetActive(true);
                    actived0.SetActive(true);
                }
            }
            else if(heroIds.Count >= 2)
            {
                inActived2.SetActive(true);
                inActived1.SetActive(true);
                if(actived)
                {
                    actived2.SetActive(true);
                    actived1.SetActive(true);
                    actived0.SetActive(true);
                }
            }
            else if(heroIds.Count >=1)
            {
                inActived1.SetActive(true);
                if(actived)
                {
                    actived1.SetActive(true);
                    actived0.SetActive(true);
                }
            }
        }

        /// <summary>
        /// 设置mask状态
        /// </summary>
        protected void SetMasked()
        {
            if (heroIds.Count == 3)
            {
                mask1.enabled = true;
                mask2.enabled = true;
                mask3.enabled = true;

                var heroId3 = heroIds[2];
                var heroId2 = heroIds[1];
                var heroId1 = heroIds[0];
                foreach (var hero in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if (hero.Value != null &&
                        hero.Value.hero != null &&
                        hero.Value.hero.heroId != this.heroId)
                    {
                        if (hero.Value.hero.heroId == heroId1) mask1.enabled = false;
                        if (hero.Value.hero.heroId == heroId2) mask2.enabled = false;
                        if (hero.Value.hero.heroId == heroId3) mask3.enabled = false;
                    }
                }
                foreach (var hero in StaticData.playerData.InstPlayerData.GetHelpCamps())
                {
                    var hero0 = StaticData.playerData.GetUIHero(hero.Value.guid);
                    if (hero0 != null &&
                        hero0.heroId != this.heroId)
                    {
                        if (hero0.heroId == heroId1) mask1.enabled = false;
                        if (hero0.heroId == heroId2) mask2.enabled = false;
                        if (hero0.heroId == heroId3) mask3.enabled = false;
                    }
                }

                if( !mask1.enabled && 
                    !mask2.enabled && 
                    !mask3.enabled)
                {
                    actived0.SetActive(true);
                    actived1.SetActive(true);
                    actived2.SetActive(true);
                    actived3.SetActive(true);
                }
                else
                {
                    actived0.SetActive(false);
                    actived1.SetActive(false);
                    actived2.SetActive(false);
                    actived3.SetActive(false);
                }
            }
            else if (heroIds.Count == 2)
            {
                mask1.enabled = true;
                mask2.enabled = true;
                mask3.enabled = false;
                
                var heroId2 = heroIds[1];
                var heroId1 = heroIds[0];
                foreach (var hero in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if (hero.Value != null &&
                        hero.Value.hero != null &&
                        hero.Value.hero.heroId != this.heroId)
                    {
                        if (hero.Value.hero.heroId == heroId1) mask1.enabled = false;
                        if (hero.Value.hero.heroId == heroId2) mask2.enabled = false;
                    }
                }
                foreach (var hero in StaticData.playerData.InstPlayerData.GetHelpCamps())
                {
                    var hero0 = StaticData.playerData.GetUIHero(hero.Value.guid);
                    if (hero0 != null &&
                        hero0.heroId != this.heroId)
                    {
                        if (hero0.heroId == heroId1) mask1.enabled = false;
                        if (hero0.heroId == heroId2) mask2.enabled = false;
                    }
                }
                if (!mask1.enabled &&
                    !mask2.enabled )
                {
                    actived0.SetActive(true);
                    actived1.SetActive(true);
                    actived2.SetActive(true);
                }
                else
                {
                    actived0.SetActive(false);
                    actived1.SetActive(false);
                    actived2.SetActive(false);
                }
            }
            else if (heroIds.Count == 1)
            {
                mask1.enabled = true;
                mask2.enabled = false;
                mask3.enabled = false;
                
                var heroId1 = heroIds[0];
                foreach (var hero in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if (hero.Value != null &&
                        hero.Value.hero != null &&
                        hero.Value.hero.heroId != this.heroId)
                    {
                        if (hero.Value.hero.heroId == heroId1) mask1.enabled = false;
                    }
                }
                foreach (var hero in StaticData.playerData.InstPlayerData.GetHelpCamps())
                {
                    var hero0 = StaticData.playerData.GetUIHero(hero.Value.guid);
                    if (hero0 != null &&
                        hero0.heroId != this.heroId)
                    {
                        if (hero0.heroId == heroId1) mask1.enabled = false;
                    }
                }

                if (!mask1.enabled )
                {
                    actived0.SetActive(true);
                    actived1.SetActive(true);
                }
                else
                {
                    actived0.SetActive(false);
                    actived1.SetActive(false);
                }
            }
        }
        protected void SetHeroIcon(string iheroId,Transform tr)
        {
            GameObject inst = GameObject.Instantiate(template);
            inst.transform.SetParent(tr);
            inst.transform.localScale = Vector3.one;
            inst.transform.localPosition = Vector3.zero;
            inst.transform.localRotation = Quaternion.Euler(Vector3.zero);
            inst.SetActive(true);
            var heroIcon = inst.GetComponent<HeroIcon>();
            heroIcon.SetHeroWithId(iheroId);
            heroIcon.m_callback = delegate
            {
                bool b = false;
                foreach (var battle in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if (battle.Value != null && battle.Value.hero != null && battle.Value.hero.heroId == iheroId)
                    {
                        PageManager.Instance.OpenPage("UIHeroPage", "guid=" + battle.Value.hero.guid);
                        var heroPage = PageManager.instance.currentPage as UIHeroPage;
                        var logicPage = heroPage.logic;
                        logicPage.SetHero(false, false, false);
                        b = true;
                        break;
                    }
                }

                if(!b)
                {
                    CommonMethod.ShowDescription(UIItemType.Hero, iheroId);
                }
            };
            cached.Add(inst);
        }
        
        private void Close()
        {
            foreach (var obj in cached)
            {
                GameObject.Destroy(obj);
            }
            cached.Clear();

            gameObject.SetActive(false);
        }
    }
}